<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import = "support.*, java.util.*, java.sql.*"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Address</title>
</head>
<body>

<%	
	Connection conn = null;
	PreparedStatement stm = null;
	ResultSet result = null;

	try {
		Class.forName("org.postgresql.Driver");
		conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
	
		Student student = (Student)session.getAttribute("student");
		student.firstName(request.getParameter("firstname"));
		student.lastName(request.getParameter("lastname"));
		student.middleName(request.getParameter("middleinitial"));
		student.citizenship(Integer.parseInt(request.getParameter("country")));

		conn.setAutoCommit(false);
		stm = conn.prepareStatement("SELECT * FROM countries_and_states WHERE NOT is_country");
		result = stm.executeQuery();
		conn.commit();
		conn.setAutoCommit(true);
	%>
	
	<h3>Previously entered in information:</h3>

	<p><%= "Full name: " + student.getFirstName() + " " + student.getMiddleName() + " " + student.getLastName() %><br/>
	<%= "Country of Citizenship: " + student.getCitizenshipName() %><p/>
  	
  	<h3>Please enter in your information:</h3> 
	
	<form method = "GET" action="location.jsp" >
		Street Address: 
		<input type = "text" name = "address"/><br/>
		City:
		<input type = "text" name = "city" style="width:70px;"/><br/>
		<%if( student.getCitizenship() == 265) {%>
			State:
			<select style="width:150px;" name = "state">
				<% while(result.next()){%>
					<option value=<%=result.getString("country_state") %>><%= result.getString("country_state") %></option>
				<%} %>
			</select><br/>
		<% } %>
		ZIP Code:
		<input type = "text" name = "zipcode" style="width:40px;"/><br/>
		<input type = "submit" value="submit" name="submit" />
	
	</form>

<% 
		result.close();
		stm.close();
		conn.close();
	}
	catch (SQLException e)
	{
		throw new RuntimeException(e);
	}
    finally {
        if (result != null) {
            try {
                result.close();
            } 
            catch (SQLException e) { }
            result = null;
        }
        if (stm != null) {
            try {
                stm.close();
            } 
            catch (SQLException e) { }
            stm = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } 
            catch (SQLException e) { }
            conn = null;
        }
    }
%>	


</body>
</html>